package com.cz.adapter.friend;

import java.lang.reflect.Field;
import java.util.ArrayList;
import com.cz.R;
import com.cz.pojo.Friend;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 好友列表适配器
 */
public class FriendsListAdapter extends BaseAdapter {

	//好友list
	private ArrayList<Friend> mList;
	private Context mContext;
	private LayoutInflater inflater;
	private FriendsListAdapter.HolderView holderView;


	public FriendsListAdapter(Context context, ArrayList<Friend> list) {
		this.mContext = context;
		this.mList = list;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mList == null ? 0 : mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList == null ? null : mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_friendslist, null);
			holderView = new FriendsListAdapter.HolderView();
			holderView.tv_user_name = (TextView) convertView.findViewById(R.id.tv_user_name);
			holderView.img_user_head = (ImageView) convertView.findViewById(R.id.img_user_head);
			convertView.setTag(holderView);
		} else {
			holderView = (FriendsListAdapter.HolderView) convertView.getTag();
		}
		Friend friend = mList.get(position);


		R.drawable drawable = new R.drawable();
		int resId=0;
		Field field= null;
		try {
			field = R.drawable.class.getField(friend.getAvatar());
			resId=(Integer)field.get(drawable);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		holderView.img_user_head.setImageResource(resId);
		holderView.tv_user_name.setText(friend.getNickName());
		return convertView;
	}
	private class HolderView {
		private TextView tv_user_name;
		private ImageView img_user_head;
	}

}
